package com.np.toiectest;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.np.toiectest.dao.CauHoiDAO;
import com.np.toiectest.dao.HinhThucDAO;
import com.np.toiectest.dao.MucDoDAO;
import com.np.toiectest.dto.CauHoiDTO;
import com.np.toiectest.dto.HinhThucDTO;
import com.np.toiectest.dto.MucDoDTO;
import com.np.toiectest.util.ConfigObject;

public class ConfigActivity extends Activity implements OnClickListener {

	public static final int REQUEST_CODE_ID_PHOTO_PART = 111;
	public static final int REQUEST_CODE_ID_READING_PART = 112;
	public static final String QUESTIONs = "Question List";

	private Spinner spinnerTestMode;
	private Spinner spinnerTestLevel;
	private EditText numberEditView;
	private Button btnStart;

	private CheckBox checkBoxAll;
	private CheckBox checkBoxPhoto;
	private CheckBox checkBoxQuestionRespone;
	private CheckBox checkBoxShortConversation;
	private CheckBox checkBoxSentenceComplete;
	private CheckBox checkBoxTextComplete;
	private CheckBox checkBoxComprehension;
	private CheckBox checkBoxShortTalk;

	private HinhThucDAO hinhThucDAO = new HinhThucDAO(this);
	private MucDoDAO mucDoDao = new MucDoDAO(this);
	private CauHoiDAO cauHoiDao = new CauHoiDAO(this);

	public static final String CONFIGURATION = "Configuration Object";

	private int numberOfQuestion;

	private List<CauHoiDTO> cauHois = new ArrayList<CauHoiDTO>();
	private ConfigObject object = new ConfigObject();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_config);

		// try {
		// getData();
		// } catch (Exception e) {
		// // TODO: handle exception
		// }
		checkBoxAll = (CheckBox) findViewById(R.id.cbAll);
		checkBoxComprehension = (CheckBox) findViewById(R.id.cbComprehension);
		checkBoxPhoto = (CheckBox) findViewById(R.id.cbPhoto);
		checkBoxQuestionRespone = (CheckBox) findViewById(R.id.cbQuestionRespone);
		checkBoxSentenceComplete = (CheckBox) findViewById(R.id.cbSentenceComplete);
		checkBoxShortConversation = (CheckBox) findViewById(R.id.cbShortConversation);
		checkBoxTextComplete = (CheckBox) findViewById(R.id.cbTextComplete);
		checkBoxShortTalk = (CheckBox) findViewById(R.id.cbShortTalk);

		spinnerTestLevel = (Spinner) findViewById(R.id.spinnerLevel);
		spinnerTestMode = (Spinner) findViewById(R.id.spinnerTestMode);

		numberEditView = (EditText) findViewById(R.id.eTextNumber);

		btnStart = (Button) findViewById(R.id.btnStart);

		loadData();
		addHandlers();
	}

	private void addHandlers() {
		btnStart.setOnClickListener(this);

	}

	private void loadData() {
		// Hinh thuc
		List<HinhThucDTO> hinhThucs = hinhThucDAO.getAll();

		ArrayAdapter<HinhThucDTO> hinhThucAdapter = new ArrayAdapter<HinhThucDTO>(
				this, android.R.layout.simple_spinner_item, hinhThucs);
		hinhThucAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		spinnerTestMode.setAdapter(hinhThucAdapter);

		// Muc do
		List<MucDoDTO> mucDos = mucDoDao.getAll();

		ArrayAdapter<MucDoDTO> mucDoAdapter = new ArrayAdapter<MucDoDTO>(this,
				android.R.layout.simple_spinner_item, mucDos);
		hinhThucAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

		spinnerTestLevel.setAdapter(mucDoAdapter);

		cauHois = cauHoiDao.getAll();
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnStart:
			if (validate()) {
				int maHinhThuc = ((HinhThucDTO) spinnerTestMode
						.getSelectedItem()).getMaHinhThuc();
				int maMucDo = ((MucDoDTO) spinnerTestLevel.getSelectedItem())
						.getMaMucDo();

				setSelectedParts();

				object.setSoLuongCauHoi(numberOfQuestion
						* object.getNumberOfParts());
				object.setMaHinhThuc(maHinhThuc);
				object.setMaMucDo(maMucDo);

				showPhotoQuestion();

			} else {
				Toast.makeText(this, "Please enter a valid number",
						Toast.LENGTH_SHORT).show();
			}
			break;
		default:
			break;
		}
	}

	private ArrayList<CauHoiDTO> getPhotoQuestion(int n) {
		ArrayList<CauHoiDTO> ds = new ArrayList<CauHoiDTO>();
		int i = 0;
		for (CauHoiDTO cauHoi : cauHois) {
			if (i + 1 == n) {
				break;
			}
			if (cauHois.get(i).getLoaiCauHoi() == 1) {
				ds.add(cauHoi);
				i++;
			}

		}
		return ds;
	}

	private ArrayList<CauHoiDTO> getReadingQuestion(int n) {
		ArrayList<CauHoiDTO> ds = new ArrayList<CauHoiDTO>();
		int i = 0;
		for (CauHoiDTO cauHoi : cauHois) {
			if (i + 1 == n) {
				break;
			}
			if (cauHoi.getLoaiCauHoi() == 2) {
				ds.add(cauHoi);
				i++;
			}

		}
		return ds;
	}

	private void showPhotoQuestion() {
		ArrayList<CauHoiDTO> ds = getPhotoQuestion(numberOfQuestion);

		Intent intent = new Intent(ConfigActivity.this,
				ListenningActivity.class);
		Bundle bundle = new Bundle();
		bundle.putSerializable(CONFIGURATION, object);
		bundle.putSerializable(QUESTIONs, ds);
		bundle.putSerializable(CONFIGURATION, object);

		intent.putExtras(bundle);
		startActivityForResult(intent, REQUEST_CODE_ID_PHOTO_PART);

	}

	private void showReadingQuestion() {
		ArrayList<CauHoiDTO> ds = getReadingQuestion(numberOfQuestion);

		Bundle bundle = new Bundle();
		bundle.putSerializable(CONFIGURATION, object);
		bundle.putSerializable(QUESTIONs, ds);
		bundle.putSerializable(CONFIGURATION, object);

		Intent intent = new Intent(ConfigActivity.this, ReadingActivity.class);
		intent.putExtras(bundle);
		startActivityForResult(intent, REQUEST_CODE_ID_READING_PART);
	}

	private void setSelectedParts() {
		if (checkBoxAll.isChecked()) {
			object.setHasAll();
			disableCheckBoxs();
		} else {
			object.setHasComprehension(checkBoxComprehension.isChecked());
			object.setHasPhotoPart(checkBoxPhoto.isChecked());
			object.setHasQuestionResponePart(checkBoxQuestionRespone
					.isChecked());
			object.setHasSentenceComplete(checkBoxSentenceComplete.isChecked());
			object.setHasShortConversation(checkBoxShortConversation
					.isChecked());
			object.setHasShortTalk(checkBoxShortTalk.isChecked());
			object.setHasTextComplete(checkBoxTextComplete.isChecked());
		}

	}

	private void disableCheckBoxs() {
		checkBoxComprehension.setEnabled(false);
		checkBoxPhoto.setEnabled(false);
		checkBoxQuestionRespone.setEnabled(false);
		checkBoxSentenceComplete.setEnabled(false);
		checkBoxShortConversation.setEnabled(false);
		checkBoxTextComplete.setEnabled(false);
		checkBoxShortTalk.setEnabled(false);

	}

	private boolean validate() {
		try {
			numberOfQuestion = Integer.parseInt(numberEditView.getText()
					.toString());
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		int currentNumber = data.getIntExtra("currentNumber", 0);
		object.setCauHoiHienTai(currentNumber);
		if (requestCode == REQUEST_CODE_ID_PHOTO_PART) {
			Toast.makeText(this,
					"FINISH PHOTO, Score : " + data.getIntExtra("score", 0),
					Toast.LENGTH_SHORT).show();
			showReadingQuestion();

			// get current question number
		}
		if (requestCode == REQUEST_CODE_ID_READING_PART) {
			Toast.makeText(this, "FINISH READING, Score: ",
					data.getIntExtra("score", 0)).show();
		}

	}
}
